<?php

//老师姓名
function getteachername($uniacid, $teacherid) {
    $teacher = pdo_fetch("SELECT * FROM" . tablename('dgjy_teacher') . "WHERE uniacid=:uniacid AND teacher_id=:teacher_id",
        array(':uniacid' => $uniacid, ':teacher_id' => $teacherid));
    if (!empty($teacher)) {
        return $teacher['teacher_name'];
    } else {
        return '未知教师';
    }
}

//图片状态
function getpicturepid($picturepid) {
    if ($picturepid == '1') {
        return "<span class='label label-success'>显示</span>";
    } elseif ($picturepid == '2') {
        return "<span class='label label-danger'>隐藏</span>";
    }
}

//会员昵称
function getnickname($uniacid, $openid) {
    $user = pdo_fetch("SELECT * FROM" . tablename('dgjy_user') . "WHERE uniacid=:uniacid AND openid=:openid", array(':uniacid' => $uniacid, ':openid' => $openid));
    if (!empty($user)) {
        return $user['nickname'];
    } else {
        return '未知用户';
    }
}

function get_fans_pic($url) {
    $hdrs      = @get_headers($url);
    $pic_valid = is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/', $hdrs[0]) : false;
    return $pic_valid ? $url : false;
}

//会员头像
function getavatar($uniacid, $openid) {
    global $_W;
    $condition  = " fans.uniacid='" . $uniacid . "' and fans.openid='" . $openid . "'";
    $sql        = "SELECT fans.nickname, mem.avatar, fans.tag "
        . "FROM " . tablename('mc_mapping_fans') . " fans "
        . "LEFT JOIN " . tablename('mc_members') . " mem ON fans.uid=mem.uid "
        . "WHERE $condition LIMIT 1";
    $member_arr = pdo_fetch($sql);
    if (get_fans_pic($member_arr['avatar'])) {
        return $member_arr['avatar'];
    }
    if (!empty($member_arr['tag']) && is_string($member_arr['tag'])) {
        if (is_base64($member_arr['tag'])) {
            $member_arr['tag'] = base64_decode($member_arr['tag']);
        }
        if (is_serialized($member_arr['tag'])) {
            $member_arr['tag'] = @iunserializer($member_arr['tag']);
        }
        if (!empty($member_arr['tag']['headimgurl'])) {
            $pic = $member_arr['avatar'];
            if (get_fans_pic($pic)) {
                return $pic;
            }
            return tomedia($member_arr['tag']['headimgurl']);
        }
    }
    $user = pdo_fetch("SELECT * FROM" . tablename('dgjy_user') . "WHERE uniacid=:uniacid AND openid=:openid", array(':uniacid' => $uniacid, ':openid' => $openid));
    $pic  = $user['avatar'];
    if (get_fans_pic($pic)) {
        return $pic;
    }
    return $_W['siteroot'] . 'web/resource/images/noavatar_middle.gif';
}

//获得话题详情获取图片列表
function getpicture($uniacid, $vid, $picture_type) {
    $picture = pdo_fetchall("SELECT * FROM" . tablename('dgjy_picture') . "WHERE uniacid=:uniacid AND picture_vid=:picture_vid AND picture_type=:picture_type AND picture_pid=:picture_pid", array(':uniacid' => $uniacid, ':picture_vid' => $vid, ':picture_type' => $picture_type, ':picture_pid' => '1'));
    $pic     = array();
    foreach ($picture as $key => $v) {
        $pic[] = $v['picture_url'];
    }
    return $pic;
}

//城市
function getcityall() {

    $citydata = array(
        array('city_id' => '1', 'city_name' => '安徽',),
        array('city_id' => '2', 'city_name' => '北京',),
        array('city_id' => '3', 'city_name' => '重庆',),
        array('city_id' => '4', 'city_name' => '福建',),
        array('city_id' => '5', 'city_name' => '甘肃',),
        array('city_id' => '6', 'city_name' => '广东',),
        array('city_id' => '7', 'city_name' => '广西',),
        array('city_id' => '8', 'city_name' => '贵州',),
        array('city_id' => '9', 'city_name' => '海南',),
        array('city_id' => '10', 'city_name' => '河北',),
        array('city_id' => '11', 'city_name' => '黑龙江',),
        array('city_id' => '12', 'city_name' => '河南',),
        array('city_id' => '13', 'city_name' => '湖北',),
        array('city_id' => '14', 'city_name' => '湖南',),
        array('city_id' => '15', 'city_name' => '江苏',),
        array('city_id' => '16', 'city_name' => '江西',),
        array('city_id' => '17', 'city_name' => '吉林',),
        array('city_id' => '18', 'city_name' => '辽宁',),
        array('city_id' => '19', 'city_name' => '内蒙古',),
        array('city_id' => '20', 'city_name' => '宁夏',),
        array('city_id' => '21', 'city_name' => '青海',),
        array('city_id' => '22', 'city_name' => '山东',),
        array('city_id' => '23', 'city_name' => '上海',),
        array('city_id' => '24', 'city_name' => '山西',),
        array('city_id' => '25', 'city_name' => '陕西',),
        array('city_id' => '26', 'city_name' => '四川',),
        array('city_id' => '27', 'city_name' => '天津',),
        array('city_id' => '28', 'city_name' => '新疆',),
        array('city_id' => '29', 'city_name' => '西藏',),
        array('city_id' => '30', 'city_name' => '云南',),
        array('city_id' => '31', 'city_name' => '浙江',),
        array('city_id' => '32', 'city_name' => '香港',),
        array('city_id' => '33', 'city_name' => '澳门',),
        array('city_id' => '34', 'city_name' => '台湾',),
    );
    return $citydata;
}

function getgaptime($oldtime) {
    $gaptime = time() - $oldtime;
    switch ($gaptime) {
        case $gaptime == 0:
            $restime = '刚刚';
            break;
        case $gaptime < 60:
            $restime = $gaptime . '秒之前';
            break;
        case $gaptime < 60 * 60:
            $restime = floor($gaptime / 60) . '分钟前';
            break;
        case $gaptime < 60 * 60 * 24:
            $restime = floor($gaptime / (60 * 60)) . '小时前';
            break;
        case $gaptime < 60 * 60 * 24 * 3;
            $restime = floor($gaptime / (60 * 60 * 24)) == 1 ? '昨天' . date('H:i', $oldtime) : '前天' . date('H:i', $oldtime);
            break;
        case $gaptime < 60 * 60 * 24 * 30:
            $restime = floor($gaptime / (60 * 60 * 24)) . '天前';
            break;
        case $gaptime < 60 * 60 * 24 * 365:
            $restime = floor($gaptime / (60 * 60 * 24 * 30)) . '月前';
            break;
        case $gaptime < 60 * 60 * 24 * 365 * 3:
            $restime = floor($gaptime / (60 * 60 * 24 * 365)) . '年前';
            break;
        default:
            $restime = date('Y-m-d', $oldtime);
    }
    return $restime;
}

?>